//
//  EFTimeSegmentView.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/3.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFTimeSegmentView.h"
#import "EFTimeView.h"
#import "EFSaleListItem.h"

@interface EFTimeSegmentView ()
@property (copy, nonatomic) ClickTimeSegmentViewBlock block;
@end

@implementation EFTimeSegmentView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.timeViews = [NSMutableArray arrayWithCapacity:5];
    }
    return self;
}

- (void)setTimeSegmentItems:(NSArray *)saleListItems selectBlock:(ClickTimeSegmentViewBlock)block{
    self.block = block;
    [self.timeViews removeAllObjects];
    NSInteger totalCount = saleListItems.count;
    CGFloat x = 0;
    for (int i = 0; i < totalCount; i++) {
        EFTimeView *timeView = [[EFTimeView alloc] initWithFrame:CGRectMake(x, 0, 75, 51) item:saleListItems[i]];
        [timeView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickSegment:)]];
        x += 75;
        [self addSubview:timeView];
        [self.timeViews addObject:timeView];
    }
}

- (void)onClickSegment:(UITapGestureRecognizer *)gesture{
    UIView *timeView = gesture.view;
    NSInteger index = [self.timeViews indexOfObject:timeView];
    if (self.block) {
        self.block(index);
    }
}

@end
